package mysql

import "gorm.io/gorm"

type Goods struct {
	gorm.Model
	GoodsTitle string  `gorm:"type:varchar(20);not null;index;comment:商品标题"`
	GoodsPrice float64 `gorm:"type:decimal(10,2);not null;comment:商品价格"`
	GoodsCover string  `gorm:"type:varchar(200);not null;comment:商品封面"`
	GoodsStock int     `gorm:"type:varchar(200);not null;comment:商品库存"`
	LikeCount  int     `gorm:"type:int;not null;comment:点赞数量"`
}

func (g *Goods) ListGoods(page, pageSize int) (list []*Goods, err error) {
	err = DB.Order("like_count desc").Offset((page - 1) * pageSize).Limit(pageSize).Find(&list).Error
	return
}

func (g *Goods) DetailGoods(goodsId int) (goods []*Goods, err error) {
	err = DB.Where("goods_id", goodsId).First(&goods).Error
	return
}

func (g *Goods) UpdateStock(goodsStock int) error {
	return DB.Model(&g).Update("goods_stock", goodsStock).Error
}
func (g *Goods) GetListTop10() (list []*Goods, err error) {
	err = DB.Order("like_count desc").Limit(10).Find(&list).Error
	return
}
